<template>
  <div class="Cardregister_container">
    <!-- 搜索条件 -->
    <div class="nav">
      <Row>
        <Col span="24">
          <h2 class="addcustomergroup_title">开卡注册来源</h2>
        </Col>
      </Row>
      <Row>
        <Col span="24">
          <div class="Integraldaily">
            <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="90">
              <Row>
                <Col span="6">
                  <FormItem label="选择颗粒度：">
                    <Row>
                      <Col span="5">
                        <RadioGroup type="button" v-model="formValidate.time_type"  class="buttongroup">
                              <Radio label="1" >日</Radio>
                              <Radio label="2" >周</Radio>
                              <Radio label="3" >月</Radio>
                              <Radio label="4" >年</Radio>
                            </RadioGroup>
                      </Col>
                    </Row>
                  </FormItem>
                </Col>
                <Col span="6" push="1">
                  <FormItem label="选择时间：">
                    <Row>
                      <Col span="5">
                        <FormItem prop="start_time" :label-width="120">
                          <DatePicker
                            type="datetime"
                            placeholder="请选择日期"
                            format="yyyy-MM-dd"
                            :value="formValidate.start_time"
                            @on-change="formValidate.start_time=$event"
                            style="width:9vw;"
                          ></DatePicker>
                        </FormItem>
                      </Col>
                      <Col span="7" style="text-align: center" push="9">~</Col>
                      <Col span="6" push="7">
                        <FormItem prop="end_time" :label-width="120">
                          <DatePicker
                            type="datetime"
                            :value="formValidate.end_time"
                            @on-change="formValidate.end_time=$event"
                            format="yyyy-MM-dd"
                            placeholder="请选择日期"
                            style="width:9vw;"
                          ></DatePicker>
                        </FormItem>
                      </Col>
                    </Row>
                  </FormItem>
                </Col>
                <Col span="8" push="4"  style="margin-left:1vw;">
                  <FormItem label="选择开卡注册来源：" prop="channel_id" :label-width="120">
                    <Select v-model="formValidate.channel_id" placeholder="请选择" style="width:170px;">
                      <Option
                        v-for="item in channels"
                        :value="item.id"
                        :key="item.value"
                      >{{ item.name }}</Option>
                  </Select>
                    </FormItem>
                </Col>
              </Row>
              <Row style="margin-top:2vh;">
                <Col span="8">
                  <Button
                    type="success"
                    style="width:10vw;"
                    @click="getReportMemberSalesByLevelList"
                  >查询</Button>
                </Col>
                <Col span="3" push="10">
                  <Button
                    type="success"
                    style="width:10vw;margin-left:1vw;"
                    @click="exportData()"
                  >下载</Button>
                </Col>
              </Row>
            </Form>
          </div>
        </Col>
      </Row>
    </div>
    <div class="chart_container" v-if="showchart == true">
      <div id="Gradememberskpi" v-if="showchart == true"></div>
    </div>
    <Row>
      <Col span="24">
        <!-- 表格 -->
        <div class="table_container">
          <div class="table_search_result">
            <Table height="300" :columns="columns" :data="formList" border size="small" ref="table" style="width:100%"></Table>
          </div>
        </div>
      </Col>
    </Row>
  </div>
</template>
<script>
import {
  getReportMemberSalesByLevel,
  exportMemberSalesByLevel,
  fetchChannelList
} from "@/api/report";

import echarts from "echarts";
export default {
  name: "Cardregister",
  data() {
    return {
      channels: [],
      showchart:false,
      pageNum: 1,
      pageSize: 10,
      total: 0,
      reportList: [],
      timeList: [],
      formList: [],
      nameList: [],
      formValidate: {
        // 查询参数
        start_time: "",
        end_time: "",
        channel_id: "",
        store: "",
        time_type: "1"
      },
      ruleValidate: {
        // 查询验证
        selectdate: [
          {
            required: true,
            type: "date",
            message: "请选择时间",
            trigger: "change"
          }
        ],
        selecttime: [
          {
            required: true,
            type: "date",
            message: "请选择时间",
            trigger: "change"
          }
        ],
        store: [
          { required: true, message: "请选择开卡注册来源", trigger: "change" }
        ]
      },
      columns: [
        {
          title: "日期",
          key: "date_str",
          align: "center",
          width:150,
           render: (h, params) => {
            return h('div', this.changeDate(this.formList[params.index].date_str));
          }
        },
        {
          title: "非广东设计1",
          key: "level_name",
          align: "center",
          minWidth:150
        },
        {
          title: "非广东设计2",
          key: "member_count",
          align: "center",
          minWidth:150
        },
        {
          title: "广东设计1",
          key: "member_percent",
          align: "center",
          minWidth:150
        },
        {
          title: "广东设计2",
          key: "sales_amount",
          align: "center",
          minWidth:150
        },
        {
          title: "维他微信",
          key: "sales_count",
          align: "center",
          minWidth:150
        },
        {
          title: "维他互动",
          key: "sales_product_qty",
          align: "center",
          minWidth:150
        },
        {
          title: "维他欢迎词",
          key: "sales_freq",
          align: "center",
          tooltip: "true",
          minWidth:150
        },
        {
          title: "维他奶互动",
          key: "avg_basket_size",
          align: "center",
          minWidth:150
        },
        {
          title: "维他奶欢迎词",
          key: "sales_freq",
          align: "center",
          tooltip: "true",
          minWidth:150
        },
        {
          title: "维他奶微博",
          key: "avg_product_qty",
          align: "center",
          tooltip: "true",
          minWidth:150
        },
        {
          title: "维他奶微信",
          key: "avg_basket_size",
          align: "center",
          minWidth:150
        },
        {
          title: "维他微博",
          key: "sales_freq",
          align: "center",
          tooltip: "true",
          minWidth:150
        },
        {
          title: "其他渠道",
          key: "avg_product_qty",
          align: "center",
          tooltip: "true",
          minWidth:150
        }
      ]
    };
  },
    created() {
    this.channelList();
  },
  methods: {
    channelList() {
      fetchChannelList().then(res => {
        if (res.data.code == 200) {
          if (res.data.data) {
            this.channels = res.data.data;
          }
        } else {
          this.$Message.error("操作失败");
        }
      });
    },
    handleSubmit(name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          this.$Message.success("查询成功!");
        } else {
          this.$Message.error("查询失败,请补充查询条件");
        }
      });
    },
   
    // 折线图
    drawmemberkpi() {
      echarts.dispose(document.getElementById("Gradememberskpi"));
      this.Gradememberskpi = echarts.init(
        document.getElementById("Gradememberskpi")
      );
      this.Gradememberskpi.setOption({
        animationDelay: function(idx) {
          return idx * 100;
        },
        tooltip: {
          trigger: "axis",
        },
        legend: {
          type: 'scroll',
          top: "95%",
          data: this.nameList,
        },
        grid: {
          top: 20
        },
        xAxis: {
          type: "category",
          data: this.timeList
        },
        yAxis: {
          interval: 10000,
          type: "value",
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
              // axisLabel:{
              //   formatter: function(value){
              //     return value/10000 + '万'
              //   }
              // }
        },
        series: this.reportList
      });
    },
    drawCharts() {
      this.drawmemberkpi();
    },
    getReportMemberSalesByLevelList() {
      this.TableLoading = true;
      this.load = true;
      if (this.formValidate.time_type == ''){
        this.$Message.error("请选择颗粒度");
        return;
      }
      if(this.formValidate.start_time > this.formValidate.end_time){
        this.$Message.error('开始时间必须小于结束时间');
        return;
      }
      if(this.formValidate.start_time == ""){
        this.$Message.error('请选择时间段');
        return;
      }
      if(this.formValidate.end_time == ""){
        this.$Message.error('请选择时间段');
        return;
      }
      if(this.formValidate.end_time == ""){
        this.$Message.error('请选择渠道');
        return;
      }

      var reqParams = {
        time_type: this.formValidate.time_type,
        start_time: this.formValidate.start_time,
        end_time: this.formValidate.end_time,
        channel_id: this.formValidate.channel_id
      };

    getReportMemberSalesByLevel(reqParams).then(res => {
          this.TableLoading = false;
          if (res.data.code == 200) {
            this.reportList = res.data.result.reportLists;
            this.timeList = res.data.result.timeList;
            this.formList = res.data.result.formList;
            this.nameList = res.data.result.nameList;
            this.showchart = true;
            setTimeout(()=>{
              this.drawCharts();
            },500)
          }else if(res.data.code == 401){
            this.$Message.error(res.data.msg);
          }else {
          this.$Message.error("操作失败");
        }
        })
        .catch(error => {
          this.$Message.error(error);
        });
    },
    exportData() {
        this.$refs.table.exportCsv({
            filename: "不同等级会员销售kpi报表"
            });
    },

    /** 格式化时间 */
    changeDate(value) {
      var dateee = new Date(value).toJSON();
      var date = new Date(+new Date(dateee)+8*3600*1000).toISOString().replace(/T/g,' ').replace(/\.[\d]{3}Z/,'');
      return date;
    },
    getNowFormatDate() {
      var date = moment().format("YYYY-MM-DD"); //当前时间
      var CurDate = moment().subtract(2, "days").format("YYYY-MM-DD"); //当前时间的前2天时间
      var startDate = moment().subtract(5, "days").format("YYYY-MM-DD"); //当前时间的前5天时间
      this.formValidate.start_time = startDate;
      this.formValidate.end_time = CurDate;
    }, 
  },
  mounted() {
    this.getNowFormatDate();
    var time = setInterval(() => {
      this.getNowFormatDate();
    }, 86400000);
    this.getReportMemberSalesByLevelList();
  },  
};
</script>
<style lang="scss" scoped>
@import "../../styles/common.scss";
.Cardregister_container {
  @include container_global;
  background-color: #fff;
  .active {
    color: #68b6ff;
    border: 1px solid #8fc3ff;
    background-color: #e6f2ff;
  }
  .nav {
    height: 25vh;
    width: 100%;
    line-height: 35px;
    background-color: #fff;
    position: relative;
    .addcustomergroup_title {
      height: 7vh;
      font-weight: bolder;
    }
    .Integraldaily{
      width: 100%;  
      .buttongroup {
        display: flex;
        justify-content: flex-start;
      }
    }
  }
  .chart_container {
    // 图表容器
    width: 100%;
    height: 60vh;
    border: 1px solid #e6f2ff;
    #Gradememberskpi {
      width: 85vw;
      height: 60vh;
    }
  }
  .table_container {
    margin-top: 2vh;
    border: 1px solid #e8edf1;
    .table_search_result {
      width: 100%;
      height: 300px;
      border: 1px solid #e8edf1;
      .t_head {
        height: 50px;
        line-height: 50px;
        border-bottom: 1px solid #e8edf1;
        .firth,
        .secth,
        .thirdth,
        .fourth,
        .fifth,
        .sixth,
        .seventh,
        .eighth,
        .ninth,
        .ten {
          @include table_border_global_right;
        }
        .firth {
          width: 8vw;
          font-weight: bolder;
        }
        .secth {
          width: 11vw;
          font-weight: bolder;
        }
        .thirdth,
        .fourth,
        .fifth,
        .sixth {
          font-weight: bolder;
          width: 8vw;
        }
        .seventh {
          width: 8vw;
          font-weight: bolder;
        }
      }
      .t_body {
        text-align: center;
        td {
          border-right: 1px solid #e8edf1;
          p {
            text-align: center;
          }
        }
      }
    }
  }
  .page {
    @include page_global;
  }
}
</style>
